Ext.define('Zm.view.chart.ChartData',{
    alias:'widget.chartdata',
    extend:'Ext.panel.Panel',
    requires:['Ext.toolbar.Toolbar'],
    
    cid:-1,
    initComponent:function(){
        var me = this,
            lang = Zm.i18n.Lang;
            
        Ext.apply(me,{
            title: '',
            iconCls:'chart',
            layout:'fit',
            items:[
            ],
            dockedItems:[{
                xtype:'toolbar',
                items:[lang.tStartDate,{
                    xtype:'datefield',
                    itemId:'startdate',
                    format:'Y-m-d'
                },lang.tEndDate,{
                    xtype:'datefield',
                    itemId:'enddate',
                    format:'Y-m-d'
                },{
                    text:lang.tSearch,
                    iconCls:'btn-search',
                    action:'search'
                },{
                    text:lang.tExport,
                    iconCls:'export',
                    hidden:true,
                    action:'export'
                }]
            }]//,
//            listeners:{
//                activate:{
//                    fn:me.genChart,
//                    single:true
//                }
//            }                            
        });
        me.callParent(arguments);
        me.genChart();
    } ,
    genChart:function(){
        var me = this,
            cm = Zm.Common,
            lang = Zm.i18n.Lang;
        Ext.Ajax.request({
            url: g_app + '/Chart/getChart',
            params: {cid:me.cid},
            scope: me,
            success:function(response,options){ 
                 var obj = Ext.decode(response.responseText,true);
                 if(obj==null) return;
                 if(obj.success){
                     me.newChart(obj.data);
                 }                
            },
            failure:cm.onAFail
         });      
    },
    newChart:function(data){
        var me = this,
            chart;
        me.setTitle(data.chartname);
        chart = Ext.create('Ext.chart.Chart', {
            style: 'background:#fff',
            animate: true,
            store: Ext.create('Ext.data.Store',{
                fields:data.fields,
                proxy: {
                    type: 'ajax',
                    url : g_app + '/Chart/getChartData',
                    reader: {
                        type: 'json',
                        root: 'data',
                        totalProperty:'msg' 
                    }
                }
            }),
            shadow: true,
            theme: 'Category1',
            legend: {
                position: 'top'
            },
            axes: [{
                type: 'Numeric',
                minimum: 0,
                position: 'left',
                fields: data.lfields,
                minorTickSteps: 1,
                grid: {
                    odd: {
                        opacity: 1,
                        fill: '#ddd',
                        stroke: '#bbb',
                        'stroke-width': 0.5
                    }
                }
            }, {
                type: 'Time',
                dateFormat:'Y-m-d H:i',
                position: 'bottom',
                fields: ['ptime']
            }],
            series: data.series
        });
        me.add(chart);
        me.doLayout();
    }
});